from fastapi import APIRouter#导入路由

#json的格式校验,1导入
from pydantic import BaseModel

#实例化路由 ,就相当于一个小的fastapi实例
api_user = APIRouter()

#2.设定类
class User(BaseModel):
    id: int
    username: str
    gender: str|None = None #忽略传值


'''
用restful风格写增删查改
'''

#查 get url:http://localhost:8000/user/1
@api_user.get("/{id}")
def getUser(id):
    return "查"

#增 post url:http://localhost:8000/user
@api_user.post("/")
def addUser(user:User):

    return f"增{user.username}"

#删 delete url:http://localhost:8000/user/1
@api_user.delete("/{id}")
def addUser(id):
    return "删"

#改 put url:http://localhost:8000/user
@api_user.put("/")
def addUser(id):
    return f"改{id}"